package com.frogs.ucenter.controller;

import java.util.Arrays;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.StringUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.util.SexEnum;
import com.frogs.system.web.api.response.Response.ResultParamEnum;
import com.frogs.ucenter.component.UserPermissionHelper;
import com.frogs.ucenter.entity.UserBaseAccountInfo;
import com.frogs.ucenter.service.InsuranceService;
import com.frogs.ucenter.util.SysStatusEnum;
/**
 * 保险接口
 */
@Controller
public class InsuranceController extends BaseController3 {
	private static Logger log = LoggerFactory.getLogger(InsuranceController.class);  
	
	private static final String INSURANCE_RECORD_ADD = "/insurance/record/add";
	private static final String INSURANCE_RECORD_LIST = "/insurance/record/list";
	private static final String INSURANCE_RECORD_DETAIL = "/insurance/record/detail";
	public static final String INSURANCE_INDEX = "/insurance/index";
	private static final String INSURANCE_VIEW = "insurance/index";
	
	@Resource
	private UserPermissionHelper userPermissionHelper;
	@Resource
	private InsuranceService insuranceService;
	 
    /**
     * 投保页
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping(INSURANCE_INDEX)
    public String index(HttpServletRequest request,HttpServletResponse response,ModelMap model){
		try {
			UserBaseAccountInfo user = userPermissionHelper.getUser(request);
			model.put("userInfo",user);
		} catch (Exception e) {
			log.error("func[index] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			
			request.setAttribute("errorMessage", e.getMessage());
			return ERROR_VIEW;
		}
        return INSURANCE_VIEW;
    }
    
    /**
     * 新增投保记录
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping(INSURANCE_RECORD_ADD)
    public JSONObject recordAdd(HttpServletRequest request,HttpServletResponse response,ModelMap model){
    	JSONObject json = new JSONObject();
    	try {
    		HttpRequestInfo requestInfo = new HttpRequestInfo(request);
    		UserBaseAccountInfo user = userPermissionHelper.getUser(request);
    		
    		int userId = user.getId();
    		String email = requestInfo.getParameter("email", "");
    		String identityCard = requestInfo.getParameter("identityCard", "");
    		String name = requestInfo.getParameter("name", "");
    		String phone = requestInfo.getParameter("phone", ""); 
    		Integer sex = requestInfo.getIntParameter("sex", SexEnum.UNKNOW.getId());
    		String code = requestInfo.getParameter("code", "");
    		if(StringUtil.isEmpty(code)){
    			json.put(ResultParamEnum.code.getParamName(), SysStatusEnum.FAIL.getCode());
    			json.put(ResultParamEnum.tip.getParamName(), "激活码不能为空");
    			return json;
    		}
    		return insuranceService.recordAdd(userId, email, identityCard, name, phone, sex, code);
    	} catch (Exception e) {
    		log.error("func[recordAdd] parameterMap[{}] exception[{} - {}] desc[fail]",//
    				new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
    	}
    	return null;
    }
    /**
     * 用户保单信息列表
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping(INSURANCE_RECORD_LIST)
    public JSONObject recordList(HttpServletRequest request,HttpServletResponse response,ModelMap model){
    	try {
    		HttpRequestInfo requestInfo = new HttpRequestInfo(request);
    		UserBaseAccountInfo user = userPermissionHelper.getUser(request);
    		
    		int userId = user.getId();
    		Integer pageNo = requestInfo.getIntParameter("pageNo", 1);
    		Integer pageSize = requestInfo.getIntParameter("pageSize", 10);
    		return insuranceService.recordList(userId, pageNo, pageSize, true);
    	} catch (Exception e) {
    		log.error("func[recordList] parameterMap[{}] exception[{} - {}] desc[fail]",//
    				new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
    	}
    	return null;
    }
    /**
     * 用户保单详情
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping(INSURANCE_RECORD_DETAIL)
    public JSONObject recordDetail(HttpServletRequest request,HttpServletResponse response,ModelMap model){
    	try {
    		HttpRequestInfo requestInfo = new HttpRequestInfo(request);
    		UserBaseAccountInfo user = userPermissionHelper.getUser(request);
    		
    		int userId = user.getId();
    		int recordId = requestInfo.getIntParameter("recordId", 0);
    		return insuranceService.recordDetail(userId, recordId);
    	} catch (Exception e) {
    		log.error("func[recordDetail] parameterMap[{}] exception[{} - {}] desc[fail]",//
    				new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
    	}
    	return null;
    }
}
